package com.angel.web.health;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.angel.core.util.Pager;
import com.angel.health.BuzDepartment;
import com.angel.health.BuzException;
import com.angel.provider.health.BuzDepartmentProvider;
import com.angel.provider.health.BuzExceptionProvider;


/**
 * @author hanlinxiu
 *
 */

@Controller
@RequestMapping("/exception")
@SessionAttributes("user")
public class BuzExceptionController {

	@Autowired
	private BuzExceptionProvider provider;
	
	@Autowired
	private BuzDepartmentProvider departmentProvider;
	
	/**
	 * 异常列表
	 * 
	 * @param buzException
	 * @param request
	 * @param pager
	 * @return
	 */
	@RequestMapping("/exception")
	public String findAll(BuzException buzException, HttpServletRequest request, Pager pager){
		
		buzException.setEnable(1);
		pager = provider.findAll(buzException, pager);
		
		request.setAttribute("data", pager);
		
		return "institutions/exception";
		
	}
	
	/**
	 * 新增页面
	 * 
	 * @param id
	 * @param from
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toAddException")
	public String toAddException(Integer id, String from, ModelMap modelMap){
		
		BuzException buzException = null;
		
		if(id != null){
			buzException = provider.findById(id);
		}
		
		//获取科室
		List<Map<String,Object>> deps = departmentProvider.findAll(new BuzDepartment());
		
		modelMap.put("from", from);
		modelMap.put("data", buzException);
		modelMap.put("deps", deps);
		
		return "institutions/exception_add";
	}
	
	
	/**
	 * 新增，更新异常
	 * 
	 * @param buzInstitutions
	 * @return  
	 */
	@RequestMapping("/addHealthException")
	@ResponseBody
	public Map<String, Object> addHealthException(@ModelAttribute("user") String user, @ModelAttribute("buzException")BuzException buzException){
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(buzException.getName()==null || buzException.getName().trim().equals("")){
			map.put("status", "error");
			map.put("msg", "异常库名称不能为空!");
			return map;
		}
		
		BuzDepartment dep = departmentProvider.findById(buzException.getDepartmentId());
		
		if(dep != null)
			buzException.setDepartmentName(dep.getName());
		
		if(buzException.getId() == null){
			buzException.setCreateBy(user);
			provider.insert(buzException);
		}else{
			buzException.setUpdateBy(user);
			provider.updateById(buzException);
		}
			
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	/**
	 * 删除异常库
	 * 
	 * @param buzException
	 * @return
	 */
	@RequestMapping("/deleteHealthException")
	@ResponseBody
	public Map<String, Object> deleteHealthException(@ModelAttribute("buzException")BuzException buzException){
		Map<String, Object> map = new HashMap<>();
		
		buzException.setEnable(0);
		provider.delById(buzException);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}
	
	
	/**
	 * 批量删除
	 *  
	 * @param checks
	 * @return
	 */
	@RequestMapping("/deleteHealthExceptionBatch")
	@ResponseBody
	public Map<String,Object> deleteHealthExceptionBatch(@RequestParam("checks[]")List<Integer> checks){
		
		Map<String, Object> map = new HashMap<>();
		
		provider.deleteByKeysSelective(checks);
		
		map.put("status", "success");
		map.put("msg", "操作成功");
		
		return map;
	}

	
	@RequestMapping("mergeException")
	public String mergeException(){
		return "institutions/mergeException";
	}
}
